package org.yaac.server.egql;

import static com.google.common.collect.Lists.transform;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import com.google.common.base.Function;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class GroupByClause implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final List<EntityProperty> items;
	
	public GroupByClause() {
		items = new LinkedList<EntityProperty>();
	}
	
	public GroupByClause add(String strItem) {
		items.add(EGQLUtil.parseEntityProperty(strItem));
		return this;
	}

	public List<EntityProperty> items() {
		return this.items;
	}

	public List<String> itemsInStr() {
		return transform(this.items, new Function<EntityProperty, String>() {
			@Override
			public String apply(EntityProperty arg0) {
				return arg0.getPropertyName();
			}
		});
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((items == null) ? 0 : items.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GroupByClause other = (GroupByClause) obj;
		if (items == null) {
			if (other.items != null)
				return false;
		} else if (!items.equals(other.items))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "GroupByClause [items=" + items + "]";
	}
}
